import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:grint/bloc/auth_cubit.dart';
import 'package:grint/data/data_init.dart';
import 'package:grint/enum/app_routes.dart';
import 'package:grint/utils/local_store.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _fadeAnimation;
  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    );

    _fadeAnimation = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(parent: _controller, curve: Curves.easeIn));

    _controller.forward();

    _initializeApp();
  }

  Future<void> _initializeApp() async {
    try {
      await LocalStore().init();

      await DataInit().initData();

      await context.read<AuthCubit>().checkLoginStatus();

      setState(() {
        _isInitialized = true;
      });

      await Future.delayed(const Duration(seconds: 2));

      if (mounted) {
        context.go(AppRoutes.home);
      }
    } catch (e) {
      context.go(AppRoutes.home);
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          Image.asset('assets/images/splash-bg.png', fit: BoxFit.cover),

          // 居中显示的内容
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(height: 157.h),
                FadeTransition(
                  opacity: _fadeAnimation,
                  child: Container(
                    width: 98.w,
                    height: 98.w,
                    decoration: ShapeDecoration(
                      color: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(24.r),
                      ),
                      shadows: [
                        BoxShadow(
                          color: Color(0x4FF6A2C7),
                          blurRadius: 5.20,
                          offset: Offset(0, 0),
                          spreadRadius: 4,
                        ),
                      ],
                    ),
                    padding: EdgeInsets.all(15.w),
                    child: Image.asset('assets/images/logo-small.png'),
                  ),
                ),

                Expanded(
                  child: Align(
                    alignment: Alignment.bottomCenter,
                    child: Padding(
                      padding: EdgeInsets.only(bottom: 50.h),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          FadeTransition(
                            opacity: _fadeAnimation,
                            child: Text(
                              'GRINT',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 26.sp,
                                fontFamily: 'Rubik One',
                                fontWeight: FontWeight.w400,
                                shadows: [
                                  Shadow(
                                    offset: Offset(0, 4),
                                    blurRadius: 4,
                                    color: Color(0xFF000000).withAlpha(25),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          SizedBox(height: 20.h),
                          if (!_isInitialized)
                            SizedBox(
                              width: 20.w,
                              height: 20.w,
                              child: CircularProgressIndicator(
                                valueColor: AlwaysStoppedAnimation<Color>(
                                  Colors.white,
                                ),
                                strokeWidth: 2.w,
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
